// created on 25.08.2007 at 10:30
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Designer;
using System.IO;
using System.Diagnostics;

public class FontEditTab : Table, iEditTab
{
	int pos;
	string fontPath = "";
	Font editing;
	Button button19;
	Button button20;
	Button button26;
	NumEntry sizeEntry;
	Entry fileEntry;
	ResNameEntry nameEntry;
//	HBox hbox6;
//	Button boldButton;
//	Button italicButton;
//	Button underlinedButton;
	Label label24;
//	Label label27;
	Label label28;
	Label label29;
	public FontEditTab(Font edit) : base(((uint)(4)), ((uint)(4)), false)
	{
		editing = edit;
        this.Name = "table2";
        // Container child table2.Gtk.Table+TableChild
        this.button19 = new Gtk.Button();
        this.button19.CanFocus = true;
        this.button19.Name = "button19";
        this.button19.UseUnderline = true;
        this.button19.Label = "Vorschau";
        this.Add(this.button19);
        Gtk.Table.TableChild w99 = ((Gtk.Table.TableChild)(this[this.button19]));
        w99.TopAttach = ((uint)(1));
        w99.BottomAttach = ((uint)(2));
        w99.LeftAttach = ((uint)(3));
        w99.RightAttach = ((uint)(4));
        w99.XOptions = ((Gtk.AttachOptions)(4));
        w99.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.button20 = new Gtk.Button();
        this.button20.CanFocus = true;
        this.button20.Name = "button20";
        this.button20.UseUnderline = true;
        this.button20.Label = "Durchsuchen";
        this.Add(this.button20);
        Gtk.Table.TableChild w100 = ((Gtk.Table.TableChild)(this[this.button20]));
        w100.TopAttach = ((uint)(1));
        w100.BottomAttach = ((uint)(2));
        w100.LeftAttach = ((uint)(2));
        w100.RightAttach = ((uint)(3));
        w100.XOptions = ((Gtk.AttachOptions)(4));
        w100.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.button26 = new Gtk.Button();
        this.button26.CanFocus = true;
        this.button26.Name = "button26";
        this.button26.UseStock = true;
        this.button26.UseUnderline = true;
        this.button26.Label = "gtk-ok";
        this.Add(this.button26);
        Gtk.Table.TableChild w101 = ((Gtk.Table.TableChild)(this[this.button26]));
        w101.TopAttach = ((uint)(4));
        w101.BottomAttach = ((uint)(5));
        w101.XOptions = ((Gtk.AttachOptions)(4));
        w101.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.nameEntry = new ResNameEntry();
        this.nameEntry.CanFocus = true;
        this.nameEntry.Name = "nameEntry";
        this.nameEntry.IsEditable = true;
        this.nameEntry.InvisibleChar = '•';
        nameEntry.ValidText = editing.Name;
        this.Add(this.nameEntry);
        Gtk.Table.TableChild w102 = ((Gtk.Table.TableChild)(this[this.nameEntry]));
        w102.LeftAttach = ((uint)(1));
        w102.RightAttach = ((uint)(2));
        w102.XOptions = ((Gtk.AttachOptions)(4));
        w102.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.sizeEntry = new NumEntry();
        sizeEntry.MinValue = 1;
        this.sizeEntry.CanFocus = true;
        this.sizeEntry.Name = "sizeEntry";
        this.sizeEntry.IsEditable = true;
        this.sizeEntry.InvisibleChar = '•';
        sizeEntry.Num = (int)editing.Settings["size"];
        this.Add(this.sizeEntry);
        Gtk.Table.TableChild w103 = ((Gtk.Table.TableChild)(this[this.sizeEntry]));
        w103.TopAttach = ((uint)(2));
        w103.BottomAttach = ((uint)(3));
        w103.LeftAttach = ((uint)(1));
        w103.RightAttach = ((uint)(2));
        w103.XOptions = ((Gtk.AttachOptions)(4));
        w103.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.fileEntry = new Entry();
        this.fileEntry.CanFocus = true;
        this.fileEntry.Name = "fileEntry";
        this.fileEntry.IsEditable = true;
        this.fileEntry.InvisibleChar = '•';
        fileEntry.Text = ((FileResource)editing.Settings["file"]).OrigFile;
        this.Add(this.fileEntry);
        Gtk.Table.TableChild w104 = ((Gtk.Table.TableChild)(this[this.fileEntry]));
        w104.TopAttach = ((uint)(1));
        w104.BottomAttach = ((uint)(2));
        w104.LeftAttach = ((uint)(1));
        w104.RightAttach = ((uint)(2));
        w104.XOptions = ((Gtk.AttachOptions)(4));
        w104.YOptions = ((Gtk.AttachOptions)(4));
//        // Container child table2.Gtk.Table+TableChild
//        this.hbox6 = new Gtk.HBox();
//        this.hbox6.Name = "hbox6";
//        // Container child hbox6.Gtk.Box+BoxChild
//        this.boldButton = new Gtk.Button();
//        this.boldButton.CanFocus = true;
//        this.boldButton.Name = "boldButton";
//        this.boldButton.UseStock = true;
//        this.boldButton.UseUnderline = true;
//        this.boldButton.Relief = ((styleFlags&1) == 1)?ReliefStyle.Normal:ReliefStyle.None;
//        this.boldButton.Label = "gtk-bold";
//        this.hbox6.Add(this.boldButton);
//        Gtk.Box.BoxChild w105 = ((Gtk.Box.BoxChild)(this.hbox6[this.boldButton]));
//        w105.Position = 0;
//        w105.Expand = false;
//        w105.Fill = false;
//        // Container child hbox6.Gtk.Box+BoxChild
//        this.italicButton = new Gtk.Button();
//        this.italicButton.CanFocus = true;
//        this.italicButton.Name = "italicButton";
//        this.italicButton.UseStock = true;
//        this.italicButton.UseUnderline = true;
//        this.italicButton.Relief = ((styleFlags&2) == 2)?ReliefStyle.Normal:ReliefStyle.None;
//        this.italicButton.Label = "gtk-italic";
//        this.hbox6.Add(this.italicButton);
//        Gtk.Box.BoxChild w106 = ((Gtk.Box.BoxChild)(this.hbox6[this.italicButton]));
//        w106.Position = 1;
//        w106.Expand = false;
//        w106.Fill = false;
//        // Container child hbox6.Gtk.Box+BoxChild
//        this.underlinedButton = new Gtk.Button();
//        this.underlinedButton.CanFocus = true;
//        this.underlinedButton.Name = "underlinedButton";
//        this.underlinedButton.UseStock = true;
//        this.underlinedButton.UseUnderline = true;
//        this.underlinedButton.Relief = ((styleFlags&4) == 4)?ReliefStyle.Normal:ReliefStyle.None;
//        this.underlinedButton.Label = "gtk-underline";
//        this.hbox6.Add(this.underlinedButton);
//        Gtk.Box.BoxChild w107 = ((Gtk.Box.BoxChild)(this.hbox6[this.underlinedButton]));
//        w107.Position = 2;
//        w107.Expand = false;
//        w107.Fill = false;
//        this.Add(this.hbox6);
//        Gtk.Table.TableChild w108 = ((Gtk.Table.TableChild)(this[this.hbox6]));
//        w108.TopAttach = ((uint)(3));
//        w108.BottomAttach = ((uint)(4));
//        w108.LeftAttach = ((uint)(1));
//        w108.RightAttach = ((uint)(2));
//        w108.XOptions = ((Gtk.AttachOptions)(4));
//        w108.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.label24 = new Gtk.Label();
        this.label24.Name = "label24";
        this.label24.LabelProp = "Name: ";
        this.Add(this.label24);
        Gtk.Table.TableChild w109 = ((Gtk.Table.TableChild)(this[this.label24]));
        w109.XOptions = ((Gtk.AttachOptions)(4));
        w109.YOptions = ((Gtk.AttachOptions)(4));
//        // Container child table2.Gtk.Table+TableChild
//        this.label27 = new Gtk.Label();
//        this.label27.Name = "label27";
//        this.label27.LabelProp = Mono.Unix.Catalog.GetString("Schriftstil: ");
//        this.Add(this.label27);
//        Gtk.Table.TableChild w110 = ((Gtk.Table.TableChild)(this[this.label27]));
//        w110.TopAttach = ((uint)(3));
//        w110.BottomAttach = ((uint)(4));
//        w110.XOptions = ((Gtk.AttachOptions)(4));
//        w110.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.label28 = new Gtk.Label();
        this.label28.Name = "label28";
        this.label28.LabelProp = "Schriftgröße: ";
        this.Add(this.label28);
        Gtk.Table.TableChild w111 = ((Gtk.Table.TableChild)(this[this.label28]));
        w111.TopAttach = ((uint)(2));
        w111.BottomAttach = ((uint)(3));
        w111.XOptions = ((Gtk.AttachOptions)(4));
        w111.YOptions = ((Gtk.AttachOptions)(4));
        // Container child table2.Gtk.Table+TableChild
        this.label29 = new Gtk.Label();
        this.label29.Name = "label29";
        this.label29.LabelProp = "Font-Datei: ";
        this.Add(this.label29);
        Gtk.Table.TableChild w112 = ((Gtk.Table.TableChild)(this[this.label29]));
        w112.TopAttach = ((uint)(1));
        w112.BottomAttach = ((uint)(2));
        w112.XOptions = ((Gtk.AttachOptions)(4));
        w112.YOptions = ((Gtk.AttachOptions)(4));
        CloseableTab ctab = new CloseableTab("Schrift: "+edit.Name);
        ctab.closeButton.Clicked += OnCloseActivated;
        pos = ResourceMgr.Instance.Win.editTabs.AppendPage(this, ctab);
        ResourceMgr.Instance.Win.editTabs.ShowAll();
//		boldButton.Clicked += OnBoldClicked;
//		italicButton.Clicked += OnItalicClicked;
//		underlinedButton.Clicked += OnUnderlinedClicked;
        button20.Clicked += OnDurchsuchenClicked;
        button19.Clicked += OnPreviewClicked;
        button26.Clicked += OnOKClicked;
        fileEntry.IsEditable = false;
	}
	protected void OnPreviewClicked(object o, EventArgs args)
	{
		if(fileEntry.Text == "") return;
		Process p = new Process();
		if(fontPath == "")
			p.StartInfo.FileName = ResourceMgr.Instance.tempDir+((FileResource)editing.Settings["file"]).TempFile;
		else
			p.StartInfo.FileName = fontPath;
		p.Start();
	}
	protected void OnDurchsuchenClicked(object o, EventArgs args)
	{
		FileChooserDialog Fcd = new FileChooserDialog ("Schriftart wählen", null, FileChooserAction.Open);
	  	Fcd.AddButton(Stock.Cancel, ResponseType.Cancel);
	  	Fcd.AddButton(Stock.Open, ResponseType.Ok);
	  	Fcd.Filter = new FileFilter();
	  	Fcd.Filter.AddPattern("*.ttf");
 		Fcd.SelectMultiple = false;
 		if(Directory.Exists("/usr/share/fonts/msttcorefonts"))
 		{
 			Fcd.SelectUri(Directory.GetFiles("/usr/share/fonts/msttcorefonts")[0]);
 		}
 		else if(Directory.Exists("C:\\Windows\\Fonts"))
 		{
 			Fcd.SelectUri(Directory.GetFiles("C:\\Windows\\Fonts")[0]);
 		}
	  	ResponseType RetVal = (ResponseType)Fcd.Run();
	  	if (RetVal == ResponseType.Ok)
	  	{
	  		fileEntry.Text = new FileInfo(Fcd.Filename).Name;
	  		fontPath = Fcd.Filename;
	  	}
	  	Fcd.Destroy();
	}
//	protected void OnBoldClicked(object o, EventArgs args)
//	{
//		styleFlags = styleFlags^1;
//		((Button)o).Relief = ((styleFlags&1) == 1)?ReliefStyle.Normal:ReliefStyle.None;
//	}
//	protected void OnItalicClicked(object o, EventArgs args)
//	{
//		styleFlags = styleFlags^2;
//		((Button)o).Relief = ((styleFlags&2) == 2)?ReliefStyle.Normal:ReliefStyle.None;
//	}
//	protected void OnUnderlinedClicked(object o, EventArgs args)
//	{
//		styleFlags = styleFlags^4;
//		((Button)o).Relief = ((styleFlags&4) == 4)?ReliefStyle.Normal:ReliefStyle.None;
//	}
	protected void OnOKClicked(object o, EventArgs args)
	{
		ResourceMgr.Instance.Win.CloseEditTab(editing,true);
	}
	protected void OnCloseActivated(object o, EventArgs args)
	{
		Gtk.MessageDialog message = new MessageDialog(ResourceMgr.Instance.Win, DialogFlags.Modal, MessageType.Question, ButtonsType.None, false, "");
		message.Text = "Sollen die Änderungen übernommen werden?";
		message.Title = "Änderungen übernehmen?";
		message.AddButton(Stock.Yes, ResponseType.Yes);
		message.AddButton(Stock.No, ResponseType.No);
		message.AddButton(Stock.Cancel, ResponseType.Cancel);
		ResponseType RetVal = (ResponseType)message.Run();
		message.Destroy();
		if(RetVal == ResponseType.Yes || RetVal == ResponseType.No)
		{
			ResourceMgr.Instance.Win.CloseEditTab(editing, (RetVal == ResponseType.Yes));
		}
	}
	public void Close(bool save)
	{
		if(save)
		{
		ResourceMgr.Instance.ChangeResourceName(editing, nameEntry.ValidText);
		editing.Settings["size"] = sizeEntry.Num;
		if(fontPath != "")
			((FileResource)editing.Settings["file"]).Load(fontPath);
		}
		
		ResourceMgr.Instance.Win.editTabs.Remove(this);
	}
	public void Reload()
	{
		nameEntry.ValidText = editing.Name;
	}
	public int GetPosition()
	{
		return pos;
	}
	public void SetPosition(int pos)
	{
		this.pos = pos;
	}
}	